﻿myApp.controller("registerController", function ($scope, $http, $modal, modalService, $sce, ngTableParams, $filter, authService, $timeout,$location) {
    document.title = "Penang Realty 世界地產 Member Registration";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 Member Registration', 'description': 'Register to become our member to enjoy extra benefits.' });
    $scope.close = function () {
        $scope.warning = null;
        $scope.error = null;
        $scope.info = null;
    }
    var startTimer = function () {
        $timeout(function () {
            $location.path('/Login')
        }, 2000);

    }

    function validateEmail(email) {
        var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        return re.test(email);
    }

    $scope.save = function () {
        if (!validation()) return;

      

        authService.saveRegistration($scope.data).then(function (response) {
            if (response.data == "Success") {
                $scope.info = "Registration successful! You will be redirected to login page in 2 secs";
                console.log(response);
                startTimer();
            }
            else
                $scope.info = response.data;
        }),
        function (response) {

            console.log(response);
        };

        
       // $http.post('api/User/save', $scope.data )
       //.success(function (result) {
       //    var output = result;
       //    if (output == "Success") {

       //    }

       //})

    }

    

    function validation()
    {
        $scope.warning = [];
        if (!$scope.data.Name)
            $scope.warning.push({ msg: "Name cannot be empty!" });
        if (!$scope.data.Email)
            $scope.warning.push({ msg: "Email must not be empty!" });
        if (!$scope.data.Handphone)
            $scope.warning.push({ msg: "Handphone must not be empty!" });
        if (!$scope.data.Password)
            $scope.warning.push({ msg: "Password must not be empty!" });
        if ($scope.data.Password !=$scope.data.ConfirmedPassword )
            $scope.warning.push({ msg: "Password and confirmed passwored must be the same!" });
        if (!validateEmail($scope.data.Email))
            $scope.warning.push({ msg: "Invalid email account!" });
        if ($scope.warning.length > 0)
            return false;
        else {
            $scope.warning = null;
            return true;
        }
    }

})